#include "editprofileview.h"
#include "changeuseravatardialog.h"
#include "changeusercoverdialog.h"
#include "libs/controls/toastlabel.h"
#include "libs/core/user/localuser.h"
#include "services/yingceservice.h"
#include "ui_editprofileview.h"
#include "user/localuser.h"
#include "usersettings.h"
#include "utils/utils.h"
#include "yingce.h"

EditProfileView::EditProfileView(QWidget* parent) : QFrame(parent), ui(new Ui::EditProfileView)
{
    ui->setupUi(this);
    ui->cover_label->setScaledContents(true);
    ui->edit_button->setEnabled(false);
    setFocusPolicy(Qt::StrongFocus);

    yc::LocalUser* user = YingCe::instance()->localUser();
    ui->name_field->setText(user->name());
    ui->nickname_edit->setText(user->nickname());
    ui->gender_combo->setCurrentIndex(user->gender());
    ui->profile_edit->setText(user->profile());

    ui->verticalLayout->removeWidget(ui->avatar_label);

    if (!user->avatar().isEmpty()) {
        const QString avatar_path =
                UserSettings::instance().resDir().absoluteFilePath(user->avatar());
        ui->avatar_label->setPixmap(utils::circlePixmap(avatar_path, QSize(96, 96)));
    }

    connect(ui->nickname_edit, &QLineEdit::editingFinished, this, [this] {
        const QString nickname = ui->nickname_edit->text().trimmed();
        if (nickname != YingCe::instance()->localUser()->nickname()) {
            ui->edit_button->setEnabled(true);
        }
    });
    connect(ui->gender_combo, &QComboBox::currentIndexChanged, this, [this](int index) {
        if (index != YingCe::instance()->localUser()->gender()) {
            ui->edit_button->setEnabled(true);
        }
    });
    connect(ui->profile_edit, &TextEdit::editFinished, this, [this] {
        const QString profile = ui->profile_edit->toPlainText().trimmed();
        if (profile != YingCe::instance()->localUser()->profile()) {
            ui->edit_button->setEnabled(true);
        }
    });
    connect(ui->edit_button, &QPushButton::clicked, this, [this] {
        ui->edit_button->setEnabled(false);
        yc::UserDetail detail;
        detail.setId(YingCe::instance()->localUser()->id());
        detail.setNickname(ui->nickname_edit->text().trimmed());
        detail.setGender(ui->gender_combo->currentIndex());
        detail.setProfile(ui->profile_edit->toPlainText());
        YingCeService::instance()->changeUser(
                detail,
                yc::ResponseHandler(
                        this,
                        [this, detail](const yc::Request& request, const yc::Response& response) {
                            Q_UNUSED(request)
                            if (response.status() != 200) {
                                yc::ToastLabel::showMessage(
                                        tr("Failed to edit the profile, the error is \"%1\".")
                                                .arg(response.msg()),
                                        this, 3000);
                            } else {
                                yc::LocalUser* user = YingCe::instance()->localUser();
                                user->setNickname(detail.nickname());
                                user->setGender(detail.gender());
                                user->setProfile(detail.profile());
                                yc::ToastLabel::showMessage(
                                        tr("Succeeded in editing this profile."), this, 3000);
                            }
                        }));
    });
    connect(ui->close_button, &QPushButton::clicked, this, &EditProfileView::closeRequested);
    connect(ui->change_cover_button, &QPushButton::clicked, this, [this] {
        const QString cover = YingCe::instance()->localUser()->cover();
        ChangeCoverDialog dlg(this);
        dlg.exec();
        if (cover != YingCe::instance()->localUser()->cover()) {
            const QString cover_path = UserSettings::instance().resDir().absoluteFilePath(
                    YingCe::instance()->localUser()->cover());
            ui->cover_label->setPixmap(utils::uniformPixmap(cover_path, ui->avatar_label->size()));
        }
    });
    connect(ui->change_avatar_button, &QPushButton::clicked, this, [this] {
        const QString avatar = YingCe::instance()->localUser()->cover();
        ChangeAvatarDialog dlg(this);
        dlg.exec();
        if (avatar != YingCe::instance()->localUser()->avatar()) {
            const QString avatar_path = UserSettings::instance().resDir().absoluteFilePath(
                    YingCe::instance()->localUser()->avatar());
            ui->avatar_label->setPixmap(utils::circlePixmap(avatar_path, QSize(96, 96)));
        }
    });
}

EditProfileView::~EditProfileView()
{
    delete ui;
}

void EditProfileView::resizeEvent(QResizeEvent* e)
{
    QFrame::resizeEvent(e);
    // cover
    if (!YingCe::instance()->localUser()->cover().isEmpty()) {
        const QString cover_path = UserSettings::instance().resDir().absoluteFilePath(
                YingCe::instance()->localUser()->cover());
        ui->cover_label->setPixmap(utils::uniformPixmap(cover_path, ui->cover_label->size()));
    }
    int x = (ui->cover_label->width() - ui->change_cover_button->width() - 10);
    int y = 10;
    ui->change_cover_button->move(QPoint(x, y));
    // avatar
    x = (ui->cover_label->width() - ui->avatar_label->width()) / 2;
    y = ui->cover_label->height() - ui->avatar_label->height() / 2;
    ui->avatar_label->move(QPoint(x, y));
    x = (ui->avatar_label->width() - ui->change_avatar_button->width()) / 2;
    y = (ui->avatar_label->height() - ui->change_avatar_button->height()) / 2;
    ui->change_avatar_button->move(QPoint(x, y));
}
